<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="${domain}">
	<typeAlias alias="${domainVariable}" type="${packageName}.${domain}"/>
	<typeAlias alias="${domainVariable}Search" type="${packageName}.${domain}Search"/>

	<resultMap id="${domainVariable}Result" class="${domainVariable}">
		<#list fieldMappers as fieldMapper>
		<result property="${fieldMapper.fieldName}" column="${fieldMapper.column.columnName}" />
		</#list>
	</resultMap>
	
	<sql id="tableName">
		${tableName}
	</sql>
	<sql id="columns">
		<#list fieldMappers as fieldMapper>${fieldMapper.column.columnName}<#if fieldMapper_has_next>, </#if></#list>
	</sql>

	<insert id="insert" parameterClass="${domainVariable}">
		INSERT INTO <include refid="tableName"/> (
			<include refid="columns"/>
		) VALUES (
			<#list fieldMappers as fieldMapper>#${fieldMapper.fieldName}#<#if fieldMapper_has_next>, </#if></#list>
		)
		<selectKey resultClass="int"> SELECT LAST_INSERT_ID() </selectKey>
	</insert>
	
	<update id="update" parameterClass="${domainVariable}">
		UPDATE <include refid="tableName"/> SET 
			<#list fieldMappers as fieldMapper>${fieldMapper.column.columnName}=#${fieldMapper.fieldName}#<#if fieldMapper_has_next>, </#if></#list>
		WHERE <#list primaryKeyMappers as primaryKeyMapper>${primaryKeyMapper.column.columnName}=#${primaryKeyMapper.fieldName}#<#if primaryKeyMapper_has_next> AND </#if></#list>
	</update>
	
	<delete id="delete" parameterClass="map">
		DELETE FROM <include refid="tableName"/>
		WHERE  <#list primaryKeyMappers as primaryKeyMapper>${primaryKeyMapper.column.columnName}=#${primaryKeyMapper.fieldName}#<#if primaryKeyMapper_has_next> AND </#if></#list>
	</delete>
	
	<select id="select" parameterClass="map" resultMap="${domainVariable}Result">
		SELECT <include refid="columns"/> 
		FROM <include refid="tableName"/> 
		WHERE <#list primaryKeyMappers as primaryKeyMapper>${primaryKeyMapper.column.columnName}=#${primaryKeyMapper.fieldName}#<#if primaryKeyMapper_has_next> AND </#if></#list>
	</select>
	
	<select id="search" parameterClass="${domainVariable}Search" resultMap="${domainVariable}Result">
		SELECT <include refid="columns"/> 
		FROM <include refid="tableName"/>
		WHERE 1=1
		<isNotEmpty prepend=" AND ">
			product_name LIKE '%#keyword#%' OR shop_name LIKE '%keyword%'
		</isNotEmpty>
		<iterate property=" orderByList " prepend=" ORDER BY " conjunction=", ">
			$orderByList[].columnName$ $orderByList[].sortType$
		</iterate>
		LIMIT #offset#, #limit#
	</select>
	
</sqlMap>